package com.example.ratelimit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.support.ResourceScriptSource;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.core.io.ClassPathResource;

@Configuration
public class RedisConfig {
    @Bean("sliding_window")
    public RedisScript<Long> defaultRedisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setResultType(Long.class);
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("/ratelimit/sliding_window.lua")));
        return redisScript;
    }

    @Bean("token_bucket")
    public RedisScript<Long> tokenBucketRedisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setResultType(Long.class);
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("/ratelimit/token_bucket.lua")));

        return redisScript;
    }

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂,包含一些Redis连接信息
        redisTemplate.setConnectionFactory(factory);
 
        // Json序列化配置
        Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jsonRedisSerializer.setObjectMapper(objectMapper);
 
        // String的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
 
        // key均采用String序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        // hsah的key也采用String序列化的方式
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // value采用json的序列化方式
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        // hash的value也采用json的序列化方式
        redisTemplate.setHashKeySerializer(jsonRedisSerializer);
 
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
